# -*- coding: utf-8 -*-
# Author    : wang_tuo
# Time      : 2022/1/13 16:53
import easyocr
import requests


def easy_ident(file, mode="b", reader=None):
    if mode == "b":  # 传图片二进制
        reader = easyocr.Reader(["en"]) if reader is None else reader
        result = reader.readtext(file)[0][1]
        return result
    elif mode == "p":  # 传图片路径
        with open(file, "rb") as f:
            bdata = f.read()
        # reader = easyocr.Reader(["ch_sim"]) if reader is None else reader # 中文简体
        reader = easyocr.Reader(["en"]) if reader is None else reader  # 英文
        result = reader.readtext(bdata)[0][1]
        return result


def test():
    img_urls = [
        "https://images-na.ssl-images-amazon.com/captcha/ddwwidnf/Captcha_dwfvxnappd.jpg",
        "https://images-na.ssl-images-amazon.com/captcha/xzqdsmvh/Captcha_ebkbxtuidw.jpg"

    ]
    reader = easyocr.Reader(["en"])
    for url in img_urls:
        bdata = requests.get(url).content
        result = reader.readtext(bdata)[0][1]
        print(url, result)


if __name__ == '__main__':
    file = "../vcode/abcd.png"
    resu = easy_ident(file, mode="p")
    print(resu)
